package com.chainyoung.component_tmperson.ui.view;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.common.view.BaseDialog;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.ui.adapter.BankAdapter;
import com.chainyoung.component_tmperson.ui.adapter.CommonSelectDialogAdapter;

import java.util.ArrayList;
import java.util.List;

public class CommonSelectDialog {

    private Context mContext;
    private AlertDialog alertDialog;


    private String title, remindInfo;

    private CommonSelectDialogAdapter adapter;


    private List<String> mData = new ArrayList<>();

    private OnSelectListener onSelectListener;


    public CommonSelectDialog(Context context, String title, List<String> datas, String remindInfo) {
        mContext = context;
        this.title = title;
        this.remindInfo = remindInfo;
        this.mData = datas;
        init();

    }


    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_common_select_dialog, null);
        int width = (int) (ScreenUtils.getScreenWidth(mContext) * 0.8);
        alertDialog = new AlertDialog.Builder(mContext)
                .setView(view)
                .setCancelable(true)
                .create();
        ListView listView = view.findViewById(R.id.list_view);
        listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvRemind = view.findViewById(R.id.tv_extra);
        adapter = new CommonSelectDialogAdapter(mContext, mData);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        tvTitle.setText(title);
        tvRemind.setVisibility(TextUtils.isEmpty(remindInfo) ? View.GONE : View.VISIBLE);
        tvRemind.setText(remindInfo);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onSelectListener != null) {
                    onSelectListener.onSelectItem(mData.get(position), position);
                    alertDialog.dismiss();
                }
            }
        });

        WindowManager m = ((Activity) mContext).getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.8);
        p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        alertDialog.getWindow().setAttributes(p);     //设置生效


    }


    public void show() {

        alertDialog.show();

    }

    public void close() {
        alertDialog.dismiss();
    }

    public OnSelectListener getOnSelectListener() {
        return onSelectListener;
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public interface OnSelectListener {

        void onSelectItem(String item, int position);
    }


}
